﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QRProcess
{
    public partial class QRProcess : Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public QRProcess()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 进行解码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDecodeOpen_Click(object sender, EventArgs e)
        {
            //openFileDialog1.InitialDirectory = "c:\\";
            DecodeOpenDialog.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|PNG Image|*.png|All files (*.*)|*.*";
            DecodeOpenDialog.FilterIndex = 5;
            DecodeOpenDialog.RestoreDirectory = true;
            DecodeOpenDialog.FileName = string.Empty;

            if (DecodeOpenDialog.ShowDialog() == DialogResult.OK)
            {
                
                String fileName = DecodeOpenDialog.FileName;
                System.Drawing.Bitmap bitmap = new Bitmap(fileName);
                System.Threading.ParameterizedThreadStart pThreadStart=new System.Threading.ParameterizedThreadStart(Binarilization);
                System.Threading.Thread BinaryThread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(pThreadStart),0);
                BinaryThread.Start(bitmap);
                //System.Threading.Thread.CurrentThread.Join();
                picDecode.Image = new Bitmap(bitmap);
                //System.Drawing.Color cSrc, cDest;
                //int width = bitmap.Width;
                //int height = bitmap.Height;
                //StringBuilder strBuilder = new StringBuilder();
                //for(int y=0;y<height;y++)
                //    for (int x = 0; x < width; x++)
                //    {
                //        cSrc = bitmap.GetPixel(x, y);
                //        //cDest =System.Drawing.Color.FromArgb( (int)(0.3 * cSrc.R + 0.59 * cSrc.G + 0.11 * cSrc.B));
                //       // bitmap.SetPixel(x, y, cDest);
                //        strBuilder.Append("(" + cSrc.R.ToString() + "  " + cSrc.G.ToString() + "  " + cSrc.B.ToString() + " )"); 
                        

                //    }
                //richTextBox1.Text = strBuilder.ToString();
                //picDecode.Image = new Bitmap(bitmap);

            }
            
        }
        private delegate void BinaryDelegate(object SrcObj);
        /// <summary>
        /// 处理图形颜色
        /// </summary>
        /// <param name="bitmap"></param>
        private void Binarilization(object SrcObj)
        {
            if (SrcObj is System.Drawing.Bitmap)
            {
                System.Drawing.Bitmap bitmap = SrcObj as System.Drawing.Bitmap;
                System.Drawing.Color cSrc, cDest;
                int width = bitmap.Width;
                int height = bitmap.Height;
                for (int y = 0; y < height; y++)
                    for (int x = 0; x < width; x++)
                    {
                        cSrc = bitmap.GetPixel(x, y);
                        if ((cSrc.R != 255 || cSrc.G != 255 || cSrc.B != 255) || (cSrc.R != 0 || cSrc.G != 0 || cSrc.B != 0))
                        {
                            cDest = System.Drawing.Color.FromArgb(255, 255, 255);
                            bitmap.SetPixel(x, y, cDest);
                        }
                    }
            }
        }
    }
}
